﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using System.Xml.Linq;

namespace GameTank
{
    public class Weapon : VisibleGameEntity
    {
        WeaponDTO detail;

        public WeaponDTO Detail
        {
            get { return detail; }
            set { detail = value; }
        }
        
        static Texture2D[] textures;
        static Texture2D[] explodingTexttures;
        static bool _int = false;
        static int nExplodingFrame;
        
        static Rectangle battleBounder;
        //SpriteBatch sBatch;

        public Weapon(Character parent, WeaponDTO newDetail, ContentManager Content)
        {
            // TODO: Construct any child components here
            detail = newDetail;
            detail.Parent =  parent;
            XDocument tempDoc = XDocument.Load(detail.FlyingModel);
            ModelDTO flyingMD = ModelDTO.Parse(tempDoc.Root);

            tempDoc = XDocument.Load(detail.ExplodingModel);
            ModelDTO explodingMD = ModelDTO.Parse(tempDoc.Root);
            nExplodingFrame = explodingMD.NFrame;
            if (!detail.IsReady)
                this.Visible = true;

            if (!_int)
            {
                battleBounder = Game1.battleBounder;
                textures = new Texture2D[flyingMD.NFrame];
                for (int i = 0; i < flyingMD.NFrame; i++)
                {
                    string tempString = flyingMD.FilePath + i.ToString("0") + "0";
                    textures[i] = Content.Load<Texture2D>(tempString);
                }

                
                explodingTexttures = new Texture2D[explodingMD.NFrame];

                for (int i = 0; i < explodingMD.NFrame; ++i)
                {
                    string tempString = explodingMD.FilePath + i.ToString("0");
                    explodingTexttures[i] = Content.Load<Texture2D>(tempString);
                }
            }
            
            //detail.Width = texture.Bounds.Width;
            //detail.Hieght = texture.Bounds.Height;
        }

        public WeaponDTO Save
        {
            get { return detail; }
        }
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            GamePlay gamePlay = Game1.gamePlay;
            if ((detail.Pos.Y > battleBounder.Height) || (detail.Pos.Y < 0)||
                 (detail.Pos.X > battleBounder.Width) || (detail.Pos.X < 0))
            {
                detail.IsExploding= true;
            }

            // Move meteor
            if (detail.IsFlying)
            {
                bool exPloding = false;
                detail.Pos += detail.Velocity;
                for (int k = 0; k< gamePlay.Childs.Count; k++)
                {
                    if ( gamePlay.Childs[k] is Character)
                    {
                        if (CheckCollision((Character)gamePlay.Childs[k])
                           && (this.detail.Parent.faction != ((Character)gamePlay.Childs[k]).faction))
                        {
                            if( !((Character)gamePlay.Childs[k]).isDead())
                            {
                                detail.Parent.acttackVictim((Character)gamePlay.Childs[k]);
                                exPloding = true;
                            }
                        }
                    }

                    TimeSpan a = new TimeSpan() ;
                    int b;
                    b = (int) a.TotalMilliseconds;

                    if (gamePlay.Childs[k] is Construction)
                    {
                        if (CheckCollision((Construction)gamePlay.Childs[k])
                            && !((Construction)gamePlay.Childs[k]).isBroken())
                        {
                            detail.Parent.acttackVictim((Construction)gamePlay.Childs[k]);
                            exPloding = true;
                        }
                    }
                }
                detail.IsExploding = exPloding;
            }

            if (detail.IsExploding)
            {
                detail.CurrentExplodingFrame++;
                if (detail.CurrentExplodingFrame == nExplodingFrame)
                {
                    detail.IsReady = true;
                }
            }

            if (detail.IsReady)
            {
                detail.CurrentExplodingFrame = -1;
                this.Visible = false;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime,SpriteBatch sBatch)
        {
            
            // Draw the meteor
            if (detail.IsReady)
            {
                // do somthing here
            }
            else{
                sBatch.Begin();
                if (detail.IsFlying)
                {
                    sBatch.Draw(textures[detail.Direction], detail.Pos, Color.White);
                }
                else{
                    if (detail.IsExploding)
                        sBatch.Draw(explodingTexttures[detail.CurrentExplodingFrame], detail.Pos, Color.White);
                }
                sBatch.End();
            }
        }

        public void fire(Vector2 startLoc, int direction)
        {
            detail.Direction = direction;

            detail.Width = textures[detail.Direction].Width;
            detail.Height = textures[detail.Direction].Height;

            Vector2 temp = new Vector2(startLoc.X - (detail.Width / 2), startLoc.Y - (detail.Height / 2));
            detail.Pos = temp;
            detail.IsFlying = true;
            this.Visible = true;
        }

        public bool CheckCollision(Character orther)
        {
            Rectangle rect = orther.bounder;
            Rectangle spriterect = new Rectangle((int)detail.Pos.X,(int)detail.Pos.Y,
                       detail.Width, detail.Height);
            return spriterect.Intersects(rect);
        }

        public bool CheckCollision(Construction orther)
        {
            Rectangle rect = orther.bounder;
            Rectangle spriterect = new Rectangle((int)detail.Pos.X, (int)detail.Pos.Y,
                       detail.Width, detail.Height);
            return spriterect.Intersects(rect);
        }

        public bool IsReady
        {
            get 
            {
                return detail.IsReady; 
            }
        }
    }
}
